﻿using AutoMapper;
using B.S.BasicData.Api.Write.Application.Command.Approval;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Write.Application.Handler.Approval
{
    public class UpdateApprovalHandler : IRequestHandler<UpdateApprovalCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<ApprovalModel> approcalRepo;
        private readonly IMapper mapper;

        public UpdateApprovalHandler(IBaseRepository<ApprovalModel> approcalRepo,IMapper mapper)
        {
            this.approcalRepo = approcalRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(UpdateApprovalCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var approval = approcalRepo.GetAll().Where(x => x.IsDelete == false).FirstOrDefault();
            approval = mapper.Map(request, approval);
            approval.UpdateName = "李四";
            approval.UpdateTime = DateTime.Now;
            approval.CreateName = "张三";
            approval.CreateTime= DateTime.Now;
            var i = approcalRepo.Update(approval);
            if (i>0)
            {
                res.Msg = "修改成功";
                res.Code = ApiEnum.Success;
                res.Data = i;
            }
            else
            {
                res.Msg = "修改失败";
                res.Code = ApiEnum.Error;
                res.Data = i;
            }
            return Task.FromResult(res);    
        }
    }
}
